#include <string.h>
#include "gkt_board.h"
#include "gkt_types.h"
#include "gkt_os.h"
#include "gkt_system.h"
#include "gkt_debug.h"
#include "gkt_fpsensor.h" 
#include "gkt_rfid.h"
#include "gkt_user.h"
#include "gkt_card.h"
#include "window_common.h"
#include "window_menu_user_add.h"
#include "window_user_mngr_common.h"


enum {
	WIDGET_ID_USER_ADD_TYPE_LIST = 0,
	WIDGET_ID_MAX
};

static const gkt_widget_list_attr_s sc_window_menu_user_add_type_list_attr = 
	GKT_WIDGET_LIST_STATIC_ATTR(0, NULL);

GKT_WINDOW_WIDGET_DESCS_DEFINE(menu_user_add_type) = {
	[WIDGET_ID_USER_ADD_TYPE_LIST] = GKT_WIDGET_LIST_DESC(
										0, 
										&sc_window_menu_user_add_type_list_attr),					
};

typedef struct window_menu_user_add_type {
	int	exit_on_reactive;
	window_user_add_data_s	*add_data;
	window_dialog_data_s 	dialog;
} window_menu_user_add_type_s;
static window_menu_user_add_type_s s_menu_user_add_type;

static int window_menu_user_add_type_full_handler(void)
{
	gkt_user_amount_s amount;
	int retval;
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;

	retval = gkt_user_get_amount(&amount, 
					s_menu_user_add_type.add_data->authority, 
					s_menu_user_add_type.add_data->type);
	if ((GKT_SUCCESS == retval)
		&& (amount.used_nums >= amount.max_nums))
	{
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(user_already_full), voice_flags, NULL);
		return GKT_EVENT_HANDLE_COMPLETE;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}

static int window_menu_user_add_type_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;
	uint32_t type, type_mask, type_nums;
	gkt_user_amount_s amount;
	gkt_widget_list_item_s temp_list_item;
	
	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				s_menu_user_add_type.exit_on_reactive = 1;
				s_menu_user_add_type.add_data = (window_user_add_data_s *)data;
				if (!s_menu_user_add_type.add_data) {
					gkt_error("window_menu_user_add_type: no user_add data!\n");
					return GKT_EPARAM;
				}
			}
			else if (minor_type == GKT_WINDOW_ACTIVE) {
					if (data) { /* if 1, first active */
						if (s_menu_user_add_type.add_data->type < GKT_USER_TYPE_MAX) {
							retval = window_menu_user_add_type_full_handler();
							return GKT_EVENT_HANDLE_COMPLETE;
						}
					}

					type_nums = 0;
					type_mask = 0;
					for (type = 0; type < GKT_USER_TYPE_MAX; type++) {
						retval = gkt_user_get_amount(&amount, 
									s_menu_user_add_type.add_data->authority, type);
						if ((GKT_SUCCESS == retval) && amount.max_nums) {
							type_nums++;
							type_mask |= 1U << type;
						}
					}

					window_common_mask_except_number_repeat_return(type_nums);

					gkt_widget_set_attr_by_id(WIDGET_ID_USER_ADD_TYPE_LIST, type_nums);
					for (type = 0; type < GKT_USER_TYPE_MAX; type++) {
						if (type_mask & (1U << type)) {
							temp_list_item.voice = (uint32_t)gc_window_user_add_type_user_voice_ids[type];
							temp_list_item.data = type;
							gkt_widget_list_add_item(WIDGET_ID_USER_ADD_TYPE_LIST, &temp_list_item);
						}
					}
				}
			break;
		case GKT_EVENT_WIDGET:
			if (GKT_WIDGET_EVENT_CLICK == minor_type) {
				s_menu_user_add_type.add_data->type = gkt_widget_list_get_item_data(WIDGET_ID_USER_ADD_TYPE_LIST, data);
				retval = window_menu_user_add_type_full_handler();
				if (GKT_EVENT_HANDLE_CONTINUE == retval) {
					gkt_window_show(WINDOW_ID_user_add, 
						(uint32_t)s_menu_user_add_type.add_data);
					retval = GKT_EVENT_HANDLE_COMPLETE;
				}
				else
					s_menu_user_add_type.exit_on_reactive = 0;
			}
			break;						
		default:	
			break;
	}

	return retval;
}

GKT_WINDOW_DESC_DEFINE(menu_user_add_type);

